package com.lc202309;

/**
 * Description:
 *
 * @Create 2023/9/19 21:58
 * @Version 1.0
 */
public class Solution8 {
    public int myAtoi(String s) {
        int length = s.length();
        int flag = 1;
        int index = 0;
        int res = 0;
        int num;
        while (index < length && s.charAt(index) == ' ') {
            index++;
        }
        if (index >= length) {
            return 0;
        }
        if (s.charAt(index) == '-') {
            flag = -1;
        }
        if (s.charAt(index) == '+' || s.charAt(index) == '-') {
            index++;
        }
        while (index < length && Character.isDigit(s.charAt(index))) {
            num = s.charAt(index) - '0';
            if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10 && num > 7)) {
                return flag > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            }
            res = res * 10 + num;
            index++;
        }
        return flag > 0 ? res : -res;
    }

    public static void main(String[] args) {
        System.out.println(Integer.parseInt("++00111"));
        System.out.println(Integer.parseInt("--001"));
    }
}
